#include "customer.hh"
#include <ctime>
#include <iomanip>
#include <fstream>
#include <sstream>

namespace MiniBank
{

    Customer::Customer(const std::string& customerName, const std::string& savingsID, const std::string& checkingID, double initialBalance_saving, double initialBalance_checking) : name(customerName), savingsAccount(savingsID, customerName, initialBalance_saving), checkingAccount(checkingID, customerName, initialBalance_checking) {}

    std::string Customer::getName() const 
    {
        return name;
    }

    SavingsAccount& Customer::getSavingsAccount()
    {
        return savingsAccount;
    }

    CheckingAccount& Customer::getCheckingAccount() 
    {
        return checkingAccount;
    }

    void Customer::displayAccounts() const 
    {
        std::cout << "This customer has the following accounts:" << std::endl;
        std::cout << "1. ";
        savingsAccount.displayInfo();
        std::cout << "2. ";
        checkingAccount.displayInfo();
    }

    void Customer::viewAllTransactions() const 
    {
        const auto& savingsTransactions = savingsAccount.getTransactions();
        const auto& checkingTransactions = checkingAccount.getTransactions();

        std::cout << "All transactions for customer " << name << ":" << std::endl;
        for (const auto& transaction : savingsTransactions) 
        {
            std::cout << "Savings Account - Type: " << transaction.getType() << ", Amount: $" << transaction.getAmount()
                      << ", Note: " << transaction.getNote() << ", ID: " << transaction.getTransactionID() << std::endl;
        }
        for (const auto& transaction : checkingTransactions) 
        {
            std::cout << "Checking Account - Type: " << transaction.getType() << ", Amount: $" << transaction.getAmount()
                      << ", Note: " << transaction.getNote() << ", ID: " << transaction.getTransactionID() << std::endl;
        }
    }

    void Customer::generateCustomerReport() const 
    {
        std::time_t now = std::time(nullptr);  //时间变量
        std::tm* localTime = std::localtime(&now);   //转为当地时间
        std::stringstream ss;
        ss << "transactions_" << name << "_"
           << std::put_time(localTime, "%Y-%m-%d") << ".txt";  //将日期按照格式流输入
        std::string filename = ss.str();

        std::ofstream file(filename);
        
        //打开文件，存入账单
        if (file.is_open()) 
        {
            file << "Transaction Report for Customer: " << name << std::endl;
            file << "----------------------------------------" << std::endl;
            file << "Savings Account Transactions:" << std::endl;
            const auto& savingsTransactions = savingsAccount.getTransactions();
            for (const auto& transaction : savingsTransactions) 
            {
                file << "Type: " << transaction.getType() << ", Amount: $" << transaction.getAmount()
                     << ", Note: " << transaction.getNote() << ", ID: " << transaction.getTransactionID() << std::endl;
            }
            file << "Checking Account Transactions:" << std::endl;
            const auto& checkingTransactions = checkingAccount.getTransactions();
            for (const auto& transaction : checkingTransactions) 
            {
                file << "Type: " << transaction.getType() << ", Amount: $" << transaction.getAmount()
                     << ", Note: " << transaction.getNote() << ", ID: " << transaction.getTransactionID() << std::endl;
            }
            file.close();
            std::cout << "Report successfully generated as \"" << filename << "\"" << std::endl;
        }
        else 
        {
            std::cout << "Unable to open file for report generation." << std::endl;
        }
    }

}   